package com.young.ysepay.service.impl;

import com.young.ysepay.dto.YsPayDTO;
import com.young.ysepay.service.YsBuildRequestParaService;
import com.young.ysepay.service.YsBusinessPramService;
import com.young.ysepay.utils.SignUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.Map;

/**
 * @Author: WANGNING
 * @Email wyn20140226@aliyun.com
 * @Date: 2019/6/4 18:24
 * @Des 获取证书路径并且签名接口
 */
@Service
public class YsBuildRequestParaServiceImpl implements YsBuildRequestParaService {
    @Autowired
    private YsBusinessPramService ysBusinessPramService;
    @Override
    public Map<String, String> buildRequestPara(Map<String, String> sParaTemp ) {

        //除去数组中的空值和签名参数
        Map<String, String> sPara = SignUtils.paraFilter(sParaTemp);
        YsPayDTO ysPayDTO=ysBusinessPramService.ysBusinessPram();
        //私钥证书路径
        String partnerCert = ysPayDTO.getPATH_PARTER_PKCS12();

        //读取证书
        InputStream pfxCertFileInputStream = null;//servletContext.getResourceAsStream(partnerCert);

        String mysign = "";
        try {
            pfxCertFileInputStream = new ClassPathResource(partnerCert).getInputStream();//加载本地的证书进行https加密传输
            //遍历以及根据重新排序

			/*
			构造静态配置变量（银盛支付配置参数）
			 */
            /*if (SignUtils.ysepayConfig == null) {
                new SignUtils(ysepayConfig);
            }*/
            String signContent = SignUtils.getSignContent(sPara);
            mysign = SignUtils.rsaSign1(signContent, sParaTemp.get("charset"), pfxCertFileInputStream,ysPayDTO);

        } catch (Exception e) {
            throw new RuntimeException("签名失败，请检查证书文件是否存在，密码是否正确");
        }

        sPara.put("sign", mysign);

        return sPara;
    }
}
